<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Sales
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */

/**
 * Abstract API2 class for order items rest
 *
 * @category   Mage
 * @package    Mage_Sales
 * @author     Magento Core Team <core@magentocommerce.com>
 */
abstract class ApiCenter_SalesApi_Model_Api2_Orderapi_Orderitem_Rest extends ApiCenter_SalesApi_Model_Api2_Orderapi_Orderitem
{
    /**#@+
     * Parameters in request used in model (usually specified in route)
     */
    const PARAM_ORDER_ID = 'id';
    /**#@-*/
    const  DEFAULT_WIDTH = "100";
    /**#@-*/
    const  DEFAULT_HEIGHT = "100";
    
    const VENDOR_ID="vendor_id";
    
    const NOIMAGE ="";
    
    const  DEFAULT_FILTER_TYPE = "simple";
    /**
     * Get order items list
     *
     * @return array
     */
    protected function _retrieve()
    {
        $data = array();
        $incrementid = $this->getRequest()->getParam("orderid");
        $vendorId  =$this->getRequest()->getParam('vendorid');
        
      try {
           $orderid= Mage::getModel("sales/order")->load($incrementid,"increment_id")->getId();
           if(!$orderid){
               $this->_critical("订单号不存在",400);
           }
            /* @var $helper ApiCenter_SalesApi_Helper_Data */
            $helper = Mage::helper("salesapi");
            $items = $helper->_businessFilterOrder($orderid,$vendorId);
           if(!$items){
                $this->_critical("没有可以分的订单",400);
            }elseif (!is_array($items)){
                $this->_critical("该订单的对应商品已经被删除，不能确认",400);
            }
            $res['status'] =1;
            $res['info'] =$items;
        }catch (Exception $e) {
            $res['status'] =0;
            $res['info'] =$e->getMessage();
        }
        return $res;
    }
    /**
     * Retrieve order items collection
     *
     * @return Mage_Sales_Model_Resource_Order_Item_Collection
     */
    protected function _retrieveCollection()
    {
          $this->_critical(self::RESOURCE_METHOD_NOT_IMPLEMENTED);
    }
    
    
    protected function _update($filteredData){
  
        $orderid = $this->getRequest()->getParam("orderid");
        $vendorId  =$this->getRequest()->getParam('vendorid');
       /* @var $helper ApiCenter_SalesApi_Helper_Data */
       $helper = Mage::helper("salesapi");
       try {
           $orderData = $helper->validateConfirmOrder($filteredData,$orderid,$vendorId);
         if(is_array($orderData)){
             $res['status'] =2;
             $res['info'] =$orderData;
         }else{
             $res['status'] =1;
             $res['info'] ="订单确认成功";
         }
           
         
        } catch (Exception $e) {
            Mage::log($e->getMessage());
            $res['status'] =0;
            $res['info'] =$e->getMessage();
        }
        $this->_render($res);
    }
    protected function _accept($filteredData){
   
        $vendorId  =$this->getRequest()->getParam('vendorid');
        /* @var $helper ApiCenter_SalesApi_Helper_Data */
        $helper = Mage::helper("salesapi");
        try {
          //  $orderData = $helper->validateConfirmOder($filteredData);
            if(!$filteredData){
                $this->_critical("order data invalid");
            }
            $res['status'] =1;
            $res['info'] =$filteredData;
        } catch (Exception $e) {
            $res['status'] =0;
            $res['info'] =$e->getMessage();
        }
        return $filteredData;
    }
}
